﻿'use strict';
app.controller('ShareController', function ($scope, $rootScope, $localStorage, $modal, WalletFactory, commonFactory) {

    $scope.title = "Wallet Manager",
    $scope.messeger = "",
    $scope.popup = function (windowClass, StyleModal, Controller, size) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: StyleModal,
            controller: Controller,
            size: size,
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                }
            }
        })
    },
    $scope.Buylists = [],
    $scope.SellLists = [],
    $scope.dataChartBuy = [],
    $scope.dataChartSales = [],
    $scope.ShareManage = [],
    $scope.WalletHistory = [],    
    $scope.lineChartBuyData = {
        labels: [],
        datasets: [
            {
                fillColor: "rgba(93, 178, 255,.4)",
                strokeColor: "rgba(93, 178, 255,.7)",
                pointColor: "rgba(93, 178, 255,.7)",
                pointStrokeColor: "#fff",
                data: $scope.dataChartBuy
            }
        ]
    };
    $scope.lineChartSellData = {
        labels: [],
        datasets: [
            {
                fillColor: "rgba(93, 178, 255,.4)",
                strokeColor: "rgba(93, 178, 255,.7)",
                pointColor: "rgba(93, 178, 255,.7)",
                pointStrokeColor: "#fff",
                data: $scope.dataChartSales
            }
        ]
    };
    getBuysShare($localStorage.user.UserId);
    getSellsShare($localStorage.user.UserId);
    GetWalletsHistory($localStorage.user.UserId,1);
    getShareManager($localStorage.user.UserId);
    new Chart(document.getElementById("buy").getContext("2d")).Line($scope.lineChartBuyData);
    new Chart(document.getElementById("sales").getContext("2d")).Line($scope.lineChartSellData);
    function getBuysShare(UserId) {
        WalletFactory.getBuysShare(UserId).then(function (resp) {
            $scope.Buylists = resp.data.Result;            
            $scope.dataChartBuy = resp.data.ChartData;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
    function getSellsShare(UserId) {
        WalletFactory.getSellsShare(UserId).then(function (resp) {
            $scope.SellLists = resp.data.Result; 
            $scope.dataChartSales = resp.data.ChartData;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
    function GetWalletsHistory(UserId,WalletId) {
        WalletFactory.GetWalletsHistory(UserId, WalletId).then(function (resp) {
            $scope.WalletHistory = resp.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
    function getShareManager(UserId) {
        WalletFactory.getShareManager(UserId).then(function (resp) {
            $scope.ShareManage = resp.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
    //console.log($scope.Buylists);   
});

app.controller('BuyShareModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $rootScope, $timeout, $http, messeger, title, commonFactory) {
    $scope.messeger = messeger,
    $scope.title = title,
    $scope.wallet = {},    
    $scope.selected = [],
    $scope.WalletList=[],    
    NotPeddingWalletList($localStorage.user.UserId);
    $scope.ok = function () {  $modalInstance.close(); },
    $scope.cancel = function () { $modalInstance.dismiss('cancel'); },
    $scope.data = { Wallet: '', Amount: '' };
    $scope.doBuyShare = function (data) {        
        if (angular.isUndefined(data) || data.WalletId == '' || data.Amount == '') {
            $scope.messeger = "Invalid Data"
        } else {
            $scope.buyshare = { Result: null, Status: false, Messeger: "" }
            $http.post($rootScope.APITransactionUrl + 'BuyShare', { WalletId: data.WalletId, Amount: data.Amount, UserId: $localStorage.user.UserId }).then(function successCallback(resp) {
                $scope.buyshare = resp.data;
                $scope.messeger = resp.data.Messeger;
                if (resp.data.Status == 1) {
                    //$state.go('app.managerpack');
                    $timeout(function () {
                        $modalInstance.close();//$route.reload();
                    }, 3600);
                }
            }, function errorCallback(resp) {
                $scope.messeger = "Contact the Administrator by supporting";
            })
        }
    };
    function NotPeddingWalletList(UserId) {
        commonFactory.NotPeddingWalletList(UserId).then(function (response) {
            $scope.WalletList = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
});

app.controller('SellShareModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $rootScope, $timeout, $http, $state, $stateParams, messeger, title, commonFactory) {
    $scope.messeger = messeger,
    $scope.title = title,
    $scope.wallet = {},
    $scope.selected = [],
    $scope.WalletList = [],
    NotPeddingWalletList($localStorage.user.UserId);
    $scope.ok = function () { $modalInstance.close(); },
    $scope.cancel = function () { $modalInstance.dismiss('cancel'); },
    $scope.data = { Wallet: '', Amount: '' };
    $scope.doSellShare = function (data) {
        if (angular.isUndefined(data) || data.WalletId == '' || data.Amount == '') {
            $scope.messeger = "Invalid Data"
        } else {                    
            $http.post($rootScope.APITransactionUrl + 'SellShare', { Amount: data.Amount, UserId: $localStorage.user.UserId }).then(function successCallback(resp) { 
                $scope.messeger = resp.data.Messeger;
                if (resp.data.Status == 1) {                        
                    $state.transitionTo($state.current, $stateParams, {
                        reload: true,
                        inherit: false,
                        notify: true
                    });
                    //$state.go('app.managerpack');
                    $timeout(function () {
                        $modalInstance.close();//$route.reload();
                    }, 3600);
                }

            }, function errorCallback(resp) {
                $scope.messeger = "Contact the Administrator by supporting";
            })          
        }
    };
    function NotPeddingWalletList(UserId) {
        commonFactory.NotPeddingWalletList(UserId).then(function (response) {
            $scope.WalletList = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
});



app.controller('AddShareWalletModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $rootScope, $timeout, $http, $state, $stateParams, messeger, title, commonFactory) {
    $scope.messeger = messeger,
    $scope.title = title,
    $scope.ShareTypes = [
        { Id: 0, Name: "Buy" },
        { Id: 1, Name: "Sell" }
    ]
    $scope.wallet = {},
    $scope.type = {},        
    $scope.selected = [],
    $scope.WalletList = [],
    NotPeddingWalletList($localStorage.user.UserId);
    $scope.ok = function () { $modalInstance.close(); },
    $scope.cancel = function () { $modalInstance.dismiss('cancel'); },
    $scope.Share = { Type: 0, Quantity: 0, UnitPrice: 0, UserId: $localStorage.user.UserId, WalletId: 0 },

    $scope.doAddShare = function (share) {
        $http.post($rootScope.APITransactionUrl + 'addShare', { Share: share, WalletId: share.WalletId }).then(function successCallback(resp) {            
            $scope.messeger = resp.data.Messeger;
            if (resp.data.Status) {
                $state.transitionTo($state.current, $stateParams, {
                    reload: true,
                    inherit: false,
                    notify: true
                });
                $timeout(function () {
                    $modalInstance.close();
                }, 3600);
               
            } else {
                $scope.messeger = resp.data.Messeger;                
            }
        }, function errorCallback(resp) {
            $scope.messeger = "ERR CONNECTION REFUSED";            
        })
    }
    function NotPeddingWalletList(UserId) {
        commonFactory.NotPeddingWalletList(UserId).then(function (response) {
            $scope.WalletList = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
});

